<?php

    /**
     * This file is part of the Intelligencer package.
     * For the full copyright and license, please view the LICENSE
     * file that was distributed with this source code.
     * 
     * @author      drapeko <roman.drapeko@gmail.com>
     * @copyright   Roman Drapeko <roman.drapeko@gmail.com>, 2009 - present
     */

    /**
     * itSimpleRules represents a list of files.
     * @author drapeko
     */
    class itSimpleRules extends itRules implements itClassRulesConvertibleI, itPersistentCacheableI {
        protected $rules = array();
        protected $basePath = '';
        
        /**
         * Adds an array of files to an existent list or just one file.
         * @param array $files
         * @return void
         */
        public function addFiles($files) {
            $this->rules = array_merge($this->rules, (array)$files);
        }
        
        /**
         * Adds one file to an existent list of files
         * @param array $files
         * @return void
         */
        public function addFile($file) {
            $this->rules[] = $file;
            return $this;
        }
        
        /**
         * (non-PHPdoc)
         * @see Intelligencer/Rules/itClassRulesConvertibleI#toClassRules($scanner)
         */
        public function toClassRules(itScannerI $scanner) {
            $classRules = new itClassRules();
            foreach($this->rules as $filePath) {
                $path = $this->basePath.$filePath;
                
                if (is_readable($path) && is_file($path)) {
                    $result = $scanner->scanFile($path);

                    foreach($result->getClassArray() as $namespace => $classes) {
                        foreach($classes as $class => $location) {
                            $classRules->addClass(array($namespace, $class), $path);
                        }
                    }
                    
                    foreach($result->getInterfaceArray() as $namespace => $classes) {
                        foreach($classes as $class => $location) {
                            $classRules->addClass(array($namespace, $class), $path);
                        }
                    }
                }
            }
                        
            return $classRules;
        }
        
        /**
         * (non-PHPdoc)
         * @see Intelligencer/LibUtil/Cache/itPersistentCacheableI#toPersistentCache()
         */
        public function toPersistentCache() {
            return $this->rules;
        }
        
        /**
         * (non-PHPdoc)
         * @see Intelligencer/LibUtil/Cache/itPersistentCacheableI#fromPersistentCache($val)
         */
        public function fromPersistentCache($rules) {
            $this->rules = $rules;
        }
        
        /**
         * Returns an array representation of the rules
         */
        public function toArray() {
            return $this->rules;
        }
        
        /**
         * Sets the prefix that will be added to all files automatically
         * @param string $basePath
         * @return itSimpleRules itself
         */
        public function setPrefix($basePath) {
            $this->basePath = $basePath;
            return $this;
        }
        
        /**
         * Returns the prefix
         * @return string
         */
        public function getPrefix() {
            return $this->basePath;
        }
    }
    
?>